package handler

import (
	v1 "SmartLibrary/api/v1"
	"SmartLibrary/internal/service"
	"SmartLibrary/pkg/bcrypt"
	"SmartLibrary/pkg/sid"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type AdminHandler struct {
	*Handler
	adminService service.AdminService
}

func NewAdminHandler(
	handler *Handler,
	adminService service.AdminService,
) *AdminHandler {
	return &AdminHandler{
		Handler:      handler,
		adminService: adminService,
	}
}

// Login 登录
// @Summary 账号登录
// @Schemes
// @Description
// @Tags 系统管理员模块
// @Accept json
// @Produce json
// @Param request body v1.AdminLoginRequest true "params"
// @Success 200 {object} v1.AdminLoginResponseData
// @Router /admin/login [post]
func (h *AdminHandler) Login(ctx *gin.Context) {
	// 请求参数
	var req v1.AdminLoginRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	token, err := h.adminService.Login(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusUnauthorized, err, nil)
		return
	}
	v1.HandleSuccess(ctx, v1.AdminLoginResponseData{
		AccessToken: token,
	})
}

func (h *AdminHandler) GetBookLists(ctx *gin.Context) {
	// 请求参数
	pageNum := ctx.DefaultQuery("pageNum", "1")
	pageSize := ctx.DefaultQuery("pageSize", "5")
	query := ctx.Query("query")
	condition := ctx.Query("condition")

	// 将 pageNum 和 pageSize 转换为整数类型
	ipageNum, err := strconv.Atoi(pageNum)
	if err != nil || ipageNum <= 0 {
		h.logger.Info(fmt.Sprintf("Error converting pageNum to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}

	ipageSize, err := strconv.Atoi(pageSize)
	if err != nil || ipageSize <= 0 {
		h.logger.Info(fmt.Sprintf("Error converting pageSize to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}

	req := v1.AdminGetListsRequest{
		Offset:    (ipageNum - 1) * ipageSize,
		Limit:     ipageSize,
		Condition: condition,
		Query:     query,
	}
	data, err := h.adminService.GetBookLists(ctx, &req)
	if err != nil {
		if err != v1.ErrAdminBookNotFound {
			v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
			return
		}
		v1.HandleSuccess(ctx, *data)
		//v1.HandleSuccess(ctx, new v1.AdminBookListResponse{})
	}

	v1.HandleSuccess(ctx, *data)
}
func (h *AdminHandler) GetBookType(ctx *gin.Context) {
	// 请求参数
	data, err := h.adminService.GetBookTypes(ctx)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, *data)
}
func (h *AdminHandler) GetBookTypeList(ctx *gin.Context) {
	// 请求参数
	pageNum := ctx.DefaultQuery("pageNum", "1")
	pageSize := ctx.DefaultQuery("pageSize", "5")
	query := ctx.Query("query")
	condition := ctx.Query("condition")

	// 将 pageNum 和 pageSize 转换为整数类型
	ipageNum, err := strconv.Atoi(pageNum)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageNum to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}

	ipageSize, err := strconv.Atoi(pageSize)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageSize to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}

	req := v1.AdminGetListsRequest{
		Offset:    (ipageNum - 1) * ipageSize,
		Limit:     ipageSize,
		Condition: condition,
		Query:     query,
	}
	data, err := h.adminService.GetBookTypeList(ctx, &req)
	if err != nil {
		if err != v1.ErrAdminBookTypeNotFound {
			v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
			return
		}
	}
	v1.HandleSuccess(ctx, *data)
}
func (h *AdminHandler) GetBookTypeInformation(ctx *gin.Context) {
	// 请求参数
	typeId := ctx.Query("typeId")
	if typeId == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	data, err := h.adminService.GetBookTypeInformation(ctx, typeId)
	if err != nil {
		if err == v1.ErrAdminBookTypeNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		} else {
			v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		}
		return
	}
	v1.HandleSuccess(ctx, data)
}

func (h *AdminHandler) AddBooks(ctx *gin.Context) {
	// 请求参数
	var req v1.AdminBook
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	number := sid.GenerateUnique11DigitNumber()
	req.BookNumber, _ = strconv.Atoi(number)
	err := h.adminService.AddBooks(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}
func (h *AdminHandler) DeleteBooks(ctx *gin.Context) {
	// 请求参数
	bookId := ctx.Query("bookId")
	if bookId == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.DeleteBooks(ctx, bookId)
	if err != nil {
		if err == v1.ErrAdminBookNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) GetBookInformation(ctx *gin.Context) {
	// 请求参数
	bookId := ctx.Query("bookId")
	if bookId == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	data, err := h.adminService.GetBookInformation(ctx, bookId)
	if err != nil {
		if err == v1.ErrAdminBookNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
			return
		} else {
			v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		}
		return
	}
	v1.HandleSuccess(ctx, data)
}

func (h *AdminHandler) PutBookInformation(ctx *gin.Context) {
	// 请求参数
	var req v1.AdminPutBook
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.PutBookInformation(ctx, &req)
	if err != nil {
		if err == v1.ErrAdminBookNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		} else {
			v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		}
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) AddBookTypes(context *gin.Context) {
	// 请求参数
	var req v1.AdminBookTypeRequest
	if err := context.ShouldBindJSON(&req); err != nil {
		v1.HandleError(context, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.AddBookTypes(context, &req)
	if err != nil {
		if err == v1.ErrAdminBookTypeDuplicate {
			v1.HandleError(context, http.StatusBadRequest, v1.ErrAdminBookTypeDuplicate, nil)
			return
		} else {
			v1.HandleError(context, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		}
		return
	}
	v1.HandleSuccess(context, nil)

}

func (h *AdminHandler) DeleteBookType(ctx *gin.Context) {
	// 请求参数
	bookId := ctx.Query("typeId")
	if bookId == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.DeleteBookType(ctx, bookId)
	if err != nil {
		if err == v1.ErrAdminBookTypeNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminBookTypeNotFound, nil)
			return
		} else {
			v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		}
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) PutBookTypeInformation(ctx *gin.Context) {
	var req v1.AdminBookTypeRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.PutBookTypeInformation(ctx, &req)
	if err != nil {
		if err == v1.ErrAdminBookTypeNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminBookTypeNotFound, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) GetLibraryCardsList(ctx *gin.Context) {
	// 请求参数
	pageNum := ctx.DefaultQuery("pageNum", "1")
	pageSize := ctx.DefaultQuery("pageSize", "5")
	query := ctx.Query("query")
	condition := ctx.Query("condition")

	// 将 pageNum 和 pageSize 转换为整数类型
	ipageNum, err := strconv.Atoi(pageNum)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageNum to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}

	ipageSize, err := strconv.Atoi(pageSize)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageSize to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	req := v1.AdminGetListsRequest{
		Offset:    (ipageNum - 1) * ipageSize,
		Limit:     ipageSize,
		Condition: condition,
		Query:     query,
	}
	data, err := h.adminService.GetLibraryCardsList(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, data)
}
func (h *AdminHandler) AddLibraryCard(ctx *gin.Context) {
	// 请求参数
	var req v1.AdminLibraryCardRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	hash, err := bcrypt.GeneratePasswordHash([]byte(req.Password))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	req.Password = hash
	CardNumber, err := strconv.ParseInt(sid.GenerateUnique11DigitNumber(), 10, 64)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	req.CardNumber = CardNumber
	err = h.adminService.AddLibraryCard(ctx, &req)
	if err != nil {
		if err == v1.ErrAdminLibraryCardUsernameAlreadyExists {
			v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrAdminLibraryCardUsernameAlreadyExists, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) LibraryCardInformation(ctx *gin.Context) {
	// 请求参数
	userId := ctx.Query("userId")
	if userId == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	data, err := h.adminService.GetLibraryCard(ctx, userId)
	if err != nil {
		if err == v1.ErrAdminLibraryCardNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, data)
}

func (h *AdminHandler) PutLibraryCardInformation(ctx *gin.Context) {
	// 请求参数
	var req v1.AdminLibraryPutCardRuleNumberRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.PutLibraryCardRuleNumber(ctx, &req)
	if err != nil {
		if err == v1.ErrAdminLibraryCardNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminLibraryCardNotFound, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) DeleteLibraryCard(ctx *gin.Context) {
	userId := ctx.Query("userId")
	if userId == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.DeleteLibraryCard(ctx, userId)
	if err != nil {
		if err == v1.ErrAdminLibraryCardNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminLibraryCardNotFound, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) GetBorrowsList(ctx *gin.Context) {
	// 请求参数
	pageNum := ctx.DefaultQuery("pageNum", "1")
	pageSize := ctx.DefaultQuery("pageSize", "5")
	query := ctx.Query("query")
	condition := ctx.Query("condition")

	// 将 pageNum 和 pageSize 转换为整数类型
	ipageNum, err := strconv.Atoi(pageNum)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageNum to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}

	ipageSize, err := strconv.Atoi(pageSize)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageSize to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}

	req := v1.AdminGetListsRequest{
		Offset:    (ipageNum - 1) * ipageSize,
		Limit:     ipageSize,
		Condition: condition,
		Query:     query,
	}
	data, err := h.adminService.GetBorrowsList(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, *data)
}

func (h *AdminHandler) GetBorrowRulesList(ctx *gin.Context) {
	// 请求参数
	pageNum := ctx.DefaultQuery("pageNum", "1")
	pageSize := ctx.DefaultQuery("pageSize", "5")
	query := ctx.Query("query")
	condition := ctx.Query("condition")

	// 将 pageNum 和 pageSize 转换为整数类型
	ipageNum, err := strconv.Atoi(pageNum)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageNum to int: %v", err))
		return
	}

	ipageSize, err := strconv.Atoi(pageSize)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageSize to int: %v", err))
		return
	}

	req := v1.AdminGetListsRequest{
		Offset:    (ipageNum - 1) * ipageSize,
		Limit:     ipageSize,
		Condition: condition,
		Query:     query,
	}
	data, err := h.adminService.GetBorrowRulesList(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, *data)
}

func (h *AdminHandler) AddBorrowRules(ctx *gin.Context) {
	//1.获取参数
	var req v1.AdminBookBorrowRulesRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.AddBorrowRules(ctx, &req)
	if err != nil {
		if err == v1.ErrAdminBookRuleIdAlreadyExists {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminBookRuleIdAlreadyExists, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) GetBorrowRules(context *gin.Context) {
	//1.获取参数
	ruleId := context.Query("ruleId")
	if ruleId == "" {
		v1.HandleError(context, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	data, err := h.adminService.GetBorrowRules(context, &ruleId)
	if err != nil {
		if err == v1.ErrAdminBookBorrowRulesNotFound {
			v1.HandleError(context, http.StatusBadRequest, v1.ErrAdminBookBorrowRulesNotFound, nil)
			return
		}
		v1.HandleError(context, http.StatusBadRequest, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(context, data)

}

func (h *AdminHandler) PutBorrowRules(ctx *gin.Context) {
	//1.获取参数
	var req v1.AdminPutBookBorrowRulesRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.PutBorrowRules(ctx, &req)
	if err != nil {
		if err == v1.ErrAdminBookBorrowRulesNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminBookBorrowRulesNotFound, nil)
			return
		} else if err == v1.ErrAdminBookRuleIdAlreadyExists {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminBookRuleIdAlreadyExists, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)

}

func (h *AdminHandler) DeleteBorrowRules(ctx *gin.Context) {
	ruleId := ctx.Query("ruleId")
	if ruleId == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.DeleteBorrowRules(ctx, ruleId)
	if err != nil {
		if err == v1.ErrAdminBookBorrowRulesNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminBookBorrowRulesNotFound, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)

}
func (h *AdminHandler) GetBookAdminList(ctx *gin.Context) {
	// 请求参数
	pageNum := ctx.DefaultQuery("pageNum", "1")
	pageSize := ctx.DefaultQuery("pageSize", "5")
	query := ctx.Query("query")
	condition := ctx.Query("condition")

	// 将 pageNum 和 pageSize 转换为整数类型
	ipageNum, err := strconv.Atoi(pageNum)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageNum to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}

	ipageSize, err := strconv.Atoi(pageSize)
	if err != nil {
		h.logger.Info(fmt.Sprintf("Error converting pageSize to int: %v", err))
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}

	req := v1.AdminGetListsRequest{
		Offset:    (ipageNum - 1) * ipageSize,
		Limit:     ipageSize,
		Condition: condition,
		Query:     query,
	}
	data, err := h.adminService.GetBookAdminList(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, *data)
}

func (h *AdminHandler) AddBookAdmin(ctx *gin.Context) {
	//1.获取参数
	var req v1.AdminBookAdminRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	str, err := bcrypt.GeneratePasswordHash([]byte(req.Password))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	req.Password = str
	err = h.adminService.AddBookAdmin(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)

}

func (h *AdminHandler) GetBookAdmin(ctx *gin.Context) {
	//1.获取参数
	adminId := ctx.Query("adminId")
	if adminId == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	data, err := h.adminService.GetBookAdmin(ctx, &adminId)
	if err != nil {
		if err == v1.ErrAdminBookAdminsNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminBookAdminsNotFound, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, data)

}

func (h *AdminHandler) PutBookAdmin(ctx *gin.Context) {
	//1.获取参数
	var req v1.AdminPutBookAdminRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	str, err := bcrypt.GeneratePasswordHash([]byte(req.Password))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	req.Password = str
	err = h.adminService.PutBookAdmin(ctx, &req)
	if err != nil {
		if err == v1.ErrAdminBookAdminsNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminBookAdminsNotFound, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) DeleteBookAdmin(ctx *gin.Context) {
	adminId := ctx.Query("adminId")
	if adminId == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.DeleteBookAdmin(ctx, adminId)
	if err != nil {
		if err == v1.ErrAdminBookAdminsNotFound {
			v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminBookAdminsNotFound, nil)
			return
		}
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)

}

func (h *AdminHandler) GetBorrowData(ctx *gin.Context) {
	data, err := h.adminService.GetBorrowsData(ctx)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, data)
}

func (h *AdminHandler) GetBorrowTypeData(context *gin.Context) {
	data, err := h.adminService.GetBorrowTypeData(context)
	if err != nil {
		v1.HandleError(context, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(context, data)
}

// AddAdmin 注册系统管理员
func (h *AdminHandler) AddAdmin(ctx *gin.Context) {
	var req v1.AdminRegisterRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	str, err := bcrypt.GeneratePasswordHash([]byte(req.Password))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	req.Password = str
	err = h.adminService.AddAdmin(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) DeleteAllBooks(ctx *gin.Context) {
	//传递参数获得一个图书id切片
	var req v1.AdminDeleteBooksRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrAdminParameterValidation, nil)
		return
	}
	err := h.adminService.DeleteAllBooks(ctx, &req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

func (h *AdminHandler) GetBorrowAllRulesList(ctx *gin.Context) {
	data, err := h.adminService.GetBorrowAllRulesList(ctx)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, data)
}
